//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ProvenanceSubscriberMessageParser
{
    using System;
    using Microsoft.Research.ProvenanceSubscriberCommon.Models;
    using Microsoft.Research.eResearch.Blackboard.Common;
    using System.Globalization;

    /// <summary>
    /// This class contains the implementation logic of the Input output
    /// parameter parser logic.
    /// </summary>
    class InputOutputParameterParser : AbstractMessageParser
    {
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="blackBoardMsgParser"></param>
        public InputOutputParameterParser(BlackboardMessageParser blackBoardMsgParser)
            : base(blackBoardMsgParser)
        {
        }

        /// <summary>
        /// This function parsers the message for input output parameter.
        /// </summary>
        /// <param name="message">BlackboardMessage</param>
        public override void Parse(BlackboardMessage message)
        {
            if (IsInputOutputEvent(message))
            {
                ProvenanceInformationModel provenenceModel = GetProvenanceInfo(message);

                InputOutputParameterModel IODetails = GetIOParameterDetails(message);

                provenenceModel.InputOutputParameter = IODetails;
                this.BlackboardMessageParser.RaiseInputOutEvent(provenenceModel);
            }
        }

        /// <summary>
        /// Get the IO details.
        /// </summary>
        /// <param name="message">BlackboardMessage</param>
        /// <returns>InputOutputParameterModel</returns>
        private static InputOutputParameterModel GetIOParameterDetails(BlackboardMessage message)
        {
            InputOutputParameterModel IOModel = new InputOutputParameterModel()
            {
                PropertyName = message["trident:FieldName"],
                Value = message["trident:ActivityPropertyValue"],
            };

            string direction = message["trident:ActivityPropertyDirection"];
            if (direction.Equals("InOut", StringComparison.OrdinalIgnoreCase))
            {
                if (IsExecutingActivityEvent(message))
                {
                    direction = "Input";
                }
                else if (IsClosedActivityEvent(message))
                {
                    direction = "Output";
                }
            }

            IOModel.Direction = direction;

            int eventOrder;
            if (!int.TryParse(message["trident:EventOrder"], out eventOrder))
            {
                eventOrder = -1;
            }
            IOModel.EventOrder = eventOrder;


            DateTime messageTime;
            try
            {
                messageTime = new DateTime(long.Parse(message["time:DateTimeDescription"], CultureInfo.InvariantCulture));
                IOModel.Timestamp = messageTime;
            }
            catch (FormatException)
            {
                //Ignore. 
            }
            catch (ArgumentNullException)
            {
                //Ignore. 
            }
            catch (OverflowException)
            {
                //Ignore. 
            }

            if (message.ContainsKey("trident:ActivityPropertyType"))
            {
                IOModel.PropertyType = message["trident:ActivityPropertyType"];
            }

            return IOModel;
        }
    }
}
